package com.linkdood.app.web.rest;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linkdood.app.domain.ThirdParty;
import com.linkdood.app.service.impl.DdioSynchronize;
import com.linkdood.app.utils.PaginationUtil;
import com.linkdood.app.vm.AccountVM;
import com.linkdood.app.service.AccountService;
import com.linkdood.app.service.ThirdPartyService;
import com.linkdood.app.service.UserService;
import com.linkdood.app.utils.ResourceUtil;
import com.linkdood.app.vm.UserVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * dood 用户
 *
 * @author YunJ
 */
@Api(value = "用户接口", tags = {"用户接口"})
@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserResource {

    private final UserService userService;

    private final AccountService accountService;

    private final ThirdPartyService thirdPartyService;

    private final DdioSynchronize ddioSynchronize;

    private static class UserResourceException extends RuntimeException {
        private UserResourceException(String message) {
            super(message);
        }
    }

    public UserResource(UserService userService, AccountService accountService, ThirdPartyService thirdPartyService, DdioSynchronize ddioSynchronize) {
        this.userService = userService;
        this.accountService = accountService;
        this.thirdPartyService = thirdPartyService;
        this.ddioSynchronize = ddioSynchronize;
    }
    @PreAuthorize("@ss.hasPermi('bxy:employee:index')")
    @ApiOperation(value="查询用户信息", notes="该用户信息为 Dood 服务器的用户信息; 查询条件可以组合使用")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType="query", name = "displayName", value = "用户名", dataType = "String"),
        @ApiImplicitParam(paramType="query", name = "phone", value = "用户手机号", dataType = "String"),
        @ApiImplicitParam(paramType="query", name = "sourceId", value = "用户 sourceId", dataType = "String"),
        @ApiImplicitParam(paramType="query", name = "orgId", value = "用户所属机构", dataType = "String")
    })
    @GetMapping
    public ResponseEntity<?> getUsers(@RequestParam(value = "displayName", required = false) String displayName,
                                      @RequestParam(value = "phone", required = false) String phone,
                                      @RequestParam(value = "sourceId", required = false) String sourceId,
                                      @RequestParam(value = "orgId", required = false) String orgId,
                                      Pageable pageable) {
        Page<UserVM> page = userService.pageUsers(displayName, phone, sourceId, orgId, PaginationUtil.getPage(pageable));
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page);
        return new ResponseEntity<>(page.getRecords(), headers, HttpStatus.OK);
    }

    @ApiOperation(value="查询用户信息")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType="query", name = "displayName", value = "用户名", dataType = "String"),
        @ApiImplicitParam(paramType="query", name = "phone", value = "用户手机号", dataType = "String"),
    })
    @GetMapping("/exclude-roles")
    public ResponseEntity<?> getUsers(@RequestParam(value = "displayName", required = false) String displayName,
                                      @RequestParam(value = "phone", required = false) String phone,
                                      // 查询不是相关角色的用户
                                      @RequestParam(value = "roles", required = false) List<String> roles,
                                      Pageable pageable) {
        Page<UserVM> page = userService.pageByNameAndPhoneAndNoRoles(displayName, phone, roles, PaginationUtil.getPage(pageable));
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page);
        return new ResponseEntity<>(page.getRecords(), headers, HttpStatus.OK);
    }

    @ApiOperation(value = "获取当前登录用户的好友信息", notes = "该信息为 Dood 服务器的用户信息")
    @GetMapping("/friends")
    public ResponseEntity<?> getFriends() {
        AccountVM userAccount = accountService.getCurrentAccountInfo();
        return ResponseEntity.ok(userService.listFriends(userAccount));
    }
    @PreAuthorize("@ss.hasPermi('bxy:ddio-setting:UserSyncData')")
    @ApiOperation(value = "同步 user 信息", notes = "同步用户信息")
    @GetMapping("/sync")
    public ResponseEntity<?> syncUser(@RequestParam Integer type) {
        ThirdParty thirdParty = thirdPartyService.getDefaultThirdParty();
        ddioSynchronize.syncUser(type, thirdParty);
        return ResponseEntity.ok(ResourceUtil.success());
    }
}
